import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-08-16
 * Time: 8:52
 */
public class MyStack {//顺序存储 底层是一个动态数组
    int[] elem;
    int usedsize;
    public final static int DEFAULT_CAPACITY =10;

    public  MyStack(){
        this.elem = new int[DEFAULT_CAPACITY];
    }

//    @Override
//    public String toString() {
//        return "MyStack{" +
//                "elem=" + Arrays.toString(elem) +
//                ", usedsize=" + usedsize +
//                '}';
//    }

    //打印
public void display(){
        if(usedsize == 0){
            System.out.println("空表");
        }else {
            for (int i = 0; i<usedsize;i++){
                System.out.print(elem[i]+" ");
            }
            System.out.println();
        }


}
    /**
     * 压栈  尾插
     */
        //判满
          private boolean isfull(){
              if(usedsize == elem.length){
                  return true;
              }
              return false;
          }
    public void push(int data){
        //判满
        if (isfull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }else {
            elem[usedsize++] = data;
        }
    }
    public int peek(){
              //判空
        if(usedsize == 0){
           throw new RuntimeException("空栈");
        }else {
            return elem[usedsize-1];
        }
    }

    /**
     * 出栈
     * @return
     */
    public int pop(){
        if(usedsize == 0){
            throw new RuntimeException("空栈");
        }else {
            return elem[usedsize--];
        }
    }
    /**
     * 判空
     */
    public boolean isEmpty(){
        return usedsize == 0;
    }
}
